home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 262_01.zip / SLPTR.C < prev    next >
Text File  |  1993-04-14  |  9KB  |  266 lines

  1. /***************************************************************
  2.  *                    Setup HP Laser Jet II                    *
  3.  *                         Robert Ramey                        *
  4.  *                         19 May 1987                         *
  5.  *                                                             *
  6.  *This program sets up Hewlett Packard laser printer.  It also *
  7.  *demonstrates how to use a menu driven code system.           *
  8.  ***************************************************************/
  9.  
  10. #include "stdio.h"
  11. #include "menu.h"
  12.  
  13. /* indexes into tables for various variables */
  14. #define PORTRAIT 0
  15. #define LANDSCAPE 1
  16. #define SIXLPI  0
  17. #define EIGHTLPI 1
  18. #define TWELVELPI 2
  19. #define TENCPI  0
  20. #define SIXTEENCPI 1
  21. #define EXECUTIVE 0
  22. #define LETTER 1
  23. #define LEGAL 2
  24. #define A4  3
  25. #define MONARCH 4
  26. #define BUSINESS 5
  27. #define DL 6
  28. #define C5 7
  29. #define COURIER 0
  30. #define LINEPRINTER 1
  31.  
  32. int l,w;    /* length and width in spaces */
  33. int tdots,bdots,ldots,rdots ;/* margins in dots */
  34. int p[11];   /* array of currently selected options */
  35.  
  36. /* names for each option selection */
  37. #define orientation p[0]    /* Portrait(0) or Landscape(1) */
  38. #define pagesize p[1]   /* Letter size is default */
  39. #define font p[2]   /* default font is courier */
  40. #define bold p[3]   /* default is not bold */
  41. #define lpi p[4]    /* six lines per inch */
  42. #define cpi p[5]    /* Ten characters per inch */
  43. #define ncopies p[6]    /* number of copies */
  44. #define top p[7]    /* number of lines */
  45. #define bottom p[8]
  46. #define left p[9]
  47. #define right p[10]
  48.  
  49. /* Length and width of various page sizes in dots */
  50. /* Taken from fig 2-2 and 2-3 of Laser Jet II Tech Ref */
  51. int length[] = {3150,3300,4200,3507,2850,2250,2704,2598};
  52. int width[] = {2175,2550,2550,2480,1237,1162,1913,1299};
  53. int lmargin[] = {50,50,50,50,50,50,50,50};
  54. int rmargin[] = {100,100,100,92,100,100,92,92};
  55. int tmargin[] = {60,60,60,60,60,60,60,60};
  56. int bmargin[] = {60,60,60,58,60,60,58,58};
  57. int charwidth[] = {30,18};    /*char width in dots */
  58. int linesperinch[] = {6,8,12};   /*characters per inch */
  59.  
  60. /* Codes for control of printer */
  61. char *ocode[] = {"0o","1o"}; /* orientation codes */
  62. char *pscode[] = {"2h1a","1h2a","2h3a","2h26a",
  63.         "3h80a","3h81a","3h90a","3h91a"};
  64. char *lpicode[] = {"6d","8d","0d"};
  65. char *fcode[] = {"\33(s3t","\33(s0t"};
  66. char *cpicode[] = {"10h","16.6h"};
  67. char *bcode[] = {"0B","3B"};
  68.  
  69. main(){
  70.     /* specify default selections */
  71.  
  72.     /* since Q/C does't permit initialization of unions */
  73.     /* do the job here */
  74.     orientation = PORTRAIT;
  75.     pagesize = LETTER;
  76.     font = COURIER;
  77.     bold = FALSE;
  78.     lpi = SIXLPI;
  79.     cpi = TENCPI;
  80.     ncopies = 1;
  81.     top = bottom = -1;
  82.     left = right = 0;
  83.     menu(&m);
  84.     exit(0);
  85. }
  86. /*****************************************************************
  87. action - this function recieves control when there is no menu
  88. or other action routine specified.  It recieves a pointer to a
  89. string containing the sequence of responses so far which brought
  90. us to this point in dialog.
  91. ******************************************************************/
  92. action(responses)
  93. char *responses;
  94. {
  95.     /* In this example,  the first character of the argument */
  96.     /* should contain a '2' */
  97.     /* The second character of the argument contains the option */
  98.     /* to be specified. */
  99.     /* The third character of the argument contains the */
  100.     /* selection made. */
  101.     /* Update to chosen option with the given selection */
  102.     p[response[1] - '1'] = response[2] - '1';
  103.     return 1;
  104. }
  105. /*************************************************************
  106. display - displays the current selection for each printer option
  107. This is unusual in that the text for the display is taken from
  108. the menus themselves.  This will not always be possible but is
  109. very convenient in cases like this one.
  110. **************************************************************/
  111. display(){
  112.     int i, charheigth, lineslost;
  113.     MENU *home;
  114.     MLINE *q, *a;
  115.  
  116.     /* if invalid combination of selections chosen simply return */
  117.     if(chksel() == 0) return 0;
  118.  
  119.     /* get address of second menu */
  120.     home = (MENU *)(m.selection[1].parameter);
  121.     fprintf(stderr, "\n");
  122.  
  123.     /* for each line in the second menu */
  124.     for(i = 0; i < 3;++i){
  125.         /* get text of line describing option */
  126.         q = home->selection + i;
  127.         /* get text of line describing selection */
  128.         a = ((MENU *)(q->parameter))->selection + p[i];
  129.         /* display "<option> is <selection>" */
  130.         /* not including numbers on menus */
  131.         fprintf(stderr,"%s is %s\n",q->answer+3,a->answer+3);
  132.     }
  133.  
  134.     /* display other information not amenable to the above */
  135.     /* approach */
  136.     fprintf(stderr,"Number of copies is %d\n", ncopies);
  137.     fprintf(stderr,"Top margin is %d lines\n", top);
  138.     fprintf(stderr,"Text space is %d lines long ",l-top-bottom);
  139.     fprintf(stderr,"at %d lines per inch\n",linesperinch[lpi]);
  140.     fprintf(stderr,"Bottom margin is %d lines\n", bottom);
  141.     charheight = 300 / linesperinch[lpi];
  142.     lineslost = (tdots - 1 + charheight) / charheight;
  143.     if(lineslost > top)
  144.         fprint(stderr,
  145.         "    NOTE: %d lines will be lost at top margin\n",
  146.         lineslost - top);
  147.     lineslost = (bdots - 1 + charheight) / charheight;
  148.     if(lineslost > bottom)
  149.         fprintf(stderr,
  150.         "    NOTE: %d lines will be lost at bottom margin\n",
  151.         lineslost - bottom);
  152.     fprintf(stderr,
  153.     "Pages are %d characters wide at %d characters per inch\n"
  154.     ,w - right - left, 300/charwidth[cpi]);
  155.     if(left)
  156.         fprintf(stderr,"    with a left margin of %d\n", left);
  157.     fprintf(stderr,"\n");
  158.     return 0;
  159. }
  160. /****************************************************************
  161. copies - Ask the operator how many copies he wants and store the
  162. answer.
  163. *****************************************************************/
  164. copies(){
  165.     ncopies = getn("\nHow many copies do you want?:");
  166.     return 0;
  167. }
  168. /***************************************************************
  169. margin - set margins
  170. ****************************************************************/
  171. margin(i)
  172. int i;
  173. {
  174.     static char *prompt[] = {
  175.         "How many rows do you want for the top margin:",
  176.         "How many rows for the bottom margin:",
  177.         "How many columns for the left margin:",
  178.         "How many columns for the right margin:"};
  179.     p[i+7] = getn(prompt[i]);
  180.     return 0;
  181. }
  182. /***************************************************************
  183. getn - Get a number from the operator.
  184. ****************************************************************/
  185. getn(prompt)
  186. char *prompt;
  187. {
  188.     char s[10];
  189.     fprintf(stderr, prompt);
  190.     return atoi(gets(s));
  191. }
  192. /**************************************************************
  193. finish - now that the operator has finally decide on which
  194. selections he wants for each selection, write the appropriate
  195. control codes to the output file
  196. ***************************************************************/
  197. finish(){
  198.     FILE *fp;
  199.  
  200.     if(!chksel()) return 0; /* check selections */
  201.  
  202.     /* emit printer control codes */
  203.     printf("\33E\33&l");    /* reset printer */
  204.     printf("%dx", ncopies);
  205.     printf(pscode[pagesize]);
  206.     printf(ocode[orientation]);
  207.     printf(lpicode[lpi]);
  208.     printf("%de",top);
  209.     printf("%df1L",l - top - bottom);
  210.     printf("\33&a%dla%dM",left,w - right);
  211.     printf(fcode[font]);
  212.     printf(cpicode[cpi]);
  213.     printf(bcode[bold]);
  214.  
  215.     /* create format skeleton file */
  216.     /* to be included in format documents */
  217.     fp = fopen("slptr.fmt", "w");
  218.     fprintf(fp, ".pl %d\n", l - top - bottom);
  219.     fprintf(fp, ".m1 0\n.m2 0\n");
  220.     fprintf(fp, ".rm %d\n", w - 8);
  221.     fprintf(fp, ".in 8\n");
  222.     fclose(fp);
  223.     return 99;
  224. }
  225. /****************************************************************
  226. chksel - check to see if any invalid combination of choices has
  227. be made.
  228. *****************************************************************/
  229. chksel(){
  230.     int i;
  231.  
  232.     switch(font){
  233.     case 0: break;
  234.     case 1:
  235.         bold = TRUE;
  236.         break;
  237.     case 2:
  238.         font = LINEPRINTER;
  239.         cpi = SIXTEENCPI;
  240.         lpi = EIGHTLPI;
  241.         break;
  242.     }
  243.     l = length[pagesize];
  244.     w = width[pagesize];
  245.     tdots = tmargin[pagesize];
  246.     bdots = bmargin[pagesize];
  247.     ldots = lmargin[pagesize];
  248.     rdots = rmargin[pagesize];
  249.  
  250.     if(pagesize > A4)
  251.         orientation = LANDSCAPE; /* envelopes are landscape */
  252.     if(orientation == LANDSCAPE){
  253.         i = l;l = w;w = i;    /* swap length and width */
  254.         i = tdots;tdots = ldots;ldots = i; /*top and left margin */
  255.         i = bdots;bdots = rdots;rdots = i; /*bottom and right margin */
  256.     }
  257.     l = l * linesperinch[lpi] / 300;    /* number of lines */
  258.     w = (w - rdots - ldots) / charwidth[cpi];   /* number of columns */
  259.     if(bottom == -1)   /* default half inch for top and bottom */
  260.         bottom = linesperinch[lpi] / 2;
  261.     if(top == -1)
  262.         top = linesperinch[lpi] / 2;
  263.  
  264.     return 1;
  265. }
  266. w;w = i;    /* swa